#include "StdAfx.h"
#include <string>
#include <Util/StringUtil.h>
#include <interface/data_op/data_op_common.h>


namespace aot{ namespace data{

bool idata_op_base::begin_transaction(bool use_global_cfg)
{
    if (use_global_cfg)
    {
        return g_db_cfg->exec("begin transaction;");
    }
    return g_db_user->exec("begin transaction;");
}

bool idata_op_base::commit_transaction(bool use_global_cfg)
{
    if (use_global_cfg)
    {
        return g_db_cfg->exec("commit transaction;");
    }

    return g_db_user->exec("commit transaction;");
}

bool idata_op_base::cal_page_info(int total, int count_per_page, int page_number, int& limit, int& offset)
{
    if ( total % count_per_page > 0 )
    {
		if ( (total / count_per_page)  > 0 )
		{
			if ( page_number == (total / count_per_page + 1) )
			{
				limit = total % count_per_page;
				offset = (page_number - 1) * count_per_page;
			}
			else
			{
				limit = count_per_page;
				offset = (page_number - 1) * count_per_page;
			}
		}
		else
		{
			limit = total % count_per_page;
			offset = 0;
		}
		
     /*   if ( page_number > 1 )
        {
            limit =  count_per_page;*/
        //    offset = (page_number - 2) * count_per_page +  total % count_per_page;
        //}
        //else
        //{
        //    limit = total % count_per_page;
        //    offset = 0;
        //}
    }
    else
    {
        limit = count_per_page;
        offset = (page_number - 1) * count_per_page;
    }
    return true;
}


}} /* end namespace aot/data*/
